package software;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import software.Mes_data;


import  software.Conn;

public class Mes_done {
		
	public void  Mes_insert(int ID_Use_Post,int ID_Use_Rel,int ID_Use_Get,String Con_Message,String Tim_Message,Boolean Flage){
		
		String sql="{ call message_insert(?,?,?,?,?,?,?) }";	
		try{
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.registerOutParameter(1,java.sql.Types.INTEGER);
			cs.setInt(2, ID_Use_Post); 
			cs.setInt(3, ID_Use_Rel); 
			cs.setInt(4, ID_Use_Get); 
			cs.setString(5,Con_Message); 
			cs.setString(6, Tim_Message); 
			cs.setBoolean(7, Flage); 
			cs.execute(); 
			Dataconn.CloseC();
			}catch(Exception e){
				e.printStackTrace();
			}
		}	
	
	public List<Mes_data> Mes_done_read(int id_Use_Get){
		List<Mes_data> messagelist1=new ArrayList();
		String sql="{call message_read(?)}";
		try{
			Conn dataconn=new Conn();
			dataconn.Create();
			CallableStatement cs=dataconn.CSt_SQL(sql);
			cs.setInt(1, id_Use_Get); 
			cs.execute();
			ResultSet rs=cs.getResultSet();
			while(rs.next())
			{
				Mes_data message1 = new Mes_data();
				message1.ID=rs.getInt(1);
				message1.ID_Use_Post=rs.getInt(2);
				message1.ID_Use_Rel=rs.getInt(3);
				message1.ID_Use_Get=rs.getInt(4);
				message1.Con_Message=rs.getString(5);
				message1.Tim_Message=rs.getString(6);
				message1.Flage=rs.getBoolean(7);
				messagelist1.add(message1);
				message1=null;
				//sum_message++;
			}
			dataconn.CloseCC();
			rs.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return messagelist1;
	}
	
	public Mes_data Mes_get_message(int i,List<Mes_data> messagelist1){
		Mes_data message= (Mes_data)messagelist1.get(i);
			return message;
	}
	
	public int get_don_sum_message(int id){
		int sum_message=0;
		String sql="{ call message_sum(?,?) }";
		try{
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.setInt(1, id); 
			cs.registerOutParameter(2,java.sql.Types.INTEGER);
			cs.execute(); 
			sum_message=cs.getInt(2);
			Dataconn.CloseCC();
			}catch(Exception e){
				e.printStackTrace();
			}
		return sum_message;
	}
	
	
	public void Mes_don_delete(int id){
		String sql="{ call friend_message_delete(?) }";
		try{
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.setInt(1, id); 
			cs.execute(); 
			Dataconn.CloseCC();
			}catch(Exception e){
				e.printStackTrace();
			}
	}
	
	//查询该留言主人是否已回复，若主人已回复，返回主人的回复的ID;未回复，返回0
	public int Mes_don_check__rel(int Id){
		int mark = 0;
		String sql="{ call check_message_rel(?,?) }";
		try{
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.setInt(1, Id); 
			cs.registerOutParameter(2,java.sql.Types.INTEGER);
			cs.execute(); 
			mark=cs.getInt(2);
			Dataconn.CloseCC();
			}catch(Exception e){
				e.printStackTrace();
			}
			return mark;
	}
	
	public Mes_data Mes_rel_read(int RelId){
		String sql="{ call message_rel(?) }";
		Mes_data message1 = new Mes_data();
		try{
			Conn dataconn=new Conn();
			dataconn.Create();
			CallableStatement cs=dataconn.CSt_SQL(sql);
			cs.setInt(1, RelId); 
			cs.execute();
			ResultSet rs=cs.getResultSet();
			while(rs.next()){
				message1.ID=rs.getInt(1);
				message1.ID_Use_Post=rs.getInt(2);
				message1.ID_Use_Rel=rs.getInt(3);
				message1.ID_Use_Get=rs.getInt(4);
				message1.Con_Message=rs.getString(5);
				message1.Tim_Message=rs.getString(6);
				message1.Flage=rs.getBoolean(7);
			}
			dataconn.CloseCC();
			rs.close();
			}catch(Exception e){
				e.printStackTrace();
			}
			return message1;
	}
	
}
